#include <stdio.h>
#include <stdlib.h>
#include <crtdbg.h>
#include "readwave.h"
#include "BarkBandSpread.h"

void main(int argc, char **argv)
{

#ifdef _DEBUG
	_CrtSetBreakAlloc(0);
#endif


	int i;
	
	char filename[256];
	strcpy(filename, argv[1]);
	
	int numSamples, nSmpRate;
	WavHead wavhead;
	short *WaveData = ReadWave(filename, &numSamples, &wavhead);
	nSmpRate = wavhead.SampleRate;
	
	
	SPREAD_BARKBASEINFO p_info;
	BarkBandSpread *Spread = new BarkBandSpread;;
	Spread->Initialize(p_info);

	int nFrameShift = p_info.framePeriod * p_info.smpRate/1e3;
	int nWinSize = p_info.winSize * p_info.smpRate/1e3;
	int numFrame = (numSamples-nWinSize)/nFrameShift+1;
	
	
	FILE *fp = fopen("bark.txt", "w");
	short *pCurWave=WaveData;
	for (i=0; i<numFrame; i++)
	{
		float fspread = Spread->ComputeSpread(pCurWave,nWinSize,nSmpRate);
		
		fprintf(fp,"%.2f\n",fspread);
		pCurWave += nFrameShift;
	}
	
	fclose(fp);

	delete []WaveData;
	delete Spread;

	
#ifdef _DEBUG
	_CrtDumpMemoryLeaks();
#endif
	
}